package jllowrey.controller;

import java.awt.Choice;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.sound.sampled.Line;
import javax.swing.event.AncestorListener;

import jllowrey.model.DecisionLinesEvent;
import jllowrey.view.Application;

/**
 * ChoiceController, Each choice has its own textfeild and controller object associated with it, therefor each control object must know its 
 * position and the event so that when actionPerformer() is invoked, you will be able to update the appropriate choice in the event.
 * this controller updated the appropriate choice in a specific event
 * 
 * @author J Lowrey
 *
 */
public class ChoiceController implements ActionListener {

	/** the position of the choice in the arrayList of choices */
	int position;
	/** the application which the controller is communicating with */
	Application view;
	/** the DecisionLines event that the controller is communicating with */
	DecisionLinesEvent event;

	ArrayList<String> namesOfLines = new ArrayList<String>();
	/** 
	 * ChoiceController constructor 
	 * 
	 * @param dle, a DecisionLines event that the controller is communicates with 
	 * @param position, the position of a choice in the arrayList of choices
	 * @param app, an application that the controller communicates with
	 */
	public ChoiceController(DecisionLinesEvent dle, int position, Application app){
		this.position = position;
		this.view = app;
		this.event = dle;
	}

	/**
	 *  called when the user presses the enter key in a textfeild, adds the new string entered into the 
	 *  textbox. Then adds that string to an array list, only if the array dosnt already contain that string
	 *  when the same number of choices is the same as the array size a button becomes visible that will enable edge adding and disable 
	 *  changes to the texboxs
	 * 
	 * @param ae
	 */
	public void actionPerformed(ActionEvent ae){				
		for (int i = 0; i < this.event.numChoices; i++){
			event.lines.get(i).setChoice(view.choices[i].getText());			
			String namedLine = event.lines.get(i).getChoice();
			if (namesOfLines.contains(namedLine)){
				System.out.println("A duplicate name please change");
			}
			else{
				namesOfLines.add(namedLine);
			}
			System.out.println(namesOfLines);
		}
		if (namesOfLines.size() ==  event.numChoices){
			view.getButton().setVisible(true);
			System.out.println("set visibility:"+namesOfLines);
		}
	}
}


